package com.limanyik.family.oss.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.limanyik.family.common.entity.BasisAlibabaOssBucket;
import com.limanyik.family.oss.mapper.BasisAlibabaOssBucketMapper;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;


@Data
@Component
public class OssAccessConfig {

    /**
     * 加载默认的账号密码
     */
    @Value("${aliyun.oss.accessKeyId}")
    private String ACCESS_KEY_ID;
    @Value("${aliyun.oss.accessKeySecret}")
    private String ACCESS_KEY_SECRET;
    @Value("${aliyun.oss.endpoint}")
    private String ENDPOINT;
    /**
     * 是否加载数据库用户配置
     */
    @Value("${aliyun.oss.isDatasourceAccess}")
    private boolean IS_DATASOURCE_ACCESS;
    /**
     * 服务器下载默认路径
     */
    private String SERVER_DOWNLOAD_URL;
    /**
     * 网络下载默认路径(不包括HTTPS前缀)
     */
    private String HTTPS_DOWNLOAD_URL;
    /**
     * 本地下载默认路径
     */
    @Value("${aliyun.oss.localFileDownloadDefaultUrl}")
    private String LOCAL_FILE_DOWNLOAD_DEFAULT_URL;

    @Autowired
    BasisAlibabaOssBucketMapper basisAlibabaOssBucketMapper;

    @Bean
    private void setAccessConfig() {
        BasisAlibabaOssBucket basisAlibabaOssBucket = basisAlibabaOssBucketMapper.selectOne(new QueryWrapper<BasisAlibabaOssBucket>().eq("DELETE_STATUS", 0).eq("ENABLE_STATUS", 0).eq("DEFAULT_STATUS", 1));
        this.ENDPOINT = basisAlibabaOssBucket.getEndpoint();
        this.HTTPS_DOWNLOAD_URL = basisAlibabaOssBucket.getBucketDomainName() + "/";
        // TODO 是否从数据库加载用户配置
        if (IS_DATASOURCE_ACCESS) {
            // 配置数据库用户...
        }
    }


}
